package com.school.manager.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.school.manager.common.Constants;
import com.school.manager.domaim.SysFile;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysFilePo;
import com.school.manager.enums.ExceptionEnum;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.exception.BussinessException;
import com.school.manager.service.ISysFileService;
import com.school.manager.utils.ConvertUtil;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：文件控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "文件")
@RequestMapping(Constants.PROGRAM_PATH + "file")
public class SysFileController {

	@Autowired
    private ISysFileService sysFileService;
	
	@Value("${file.upload.path}")
	private String prePath;
	
	
	@PostMapping("photo")
    @ApiOperation("新增头像")
    @ApiImplicitParam(name = "文件", value = "sysFilePo", dataTypeClass = SysFilePo.class)
    public ResponseEntity photo(@RequestPart("file") MultipartFile file) {
		String extName = FileUtil.extName(file.getOriginalFilename());
		String fileName = IdUtil.fastSimpleUUID() + StrUtil.DOT + extName;
		String filePath = prePath + fileName;
		try {
			file.transferTo(new File(filePath));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
        return ResponseEnum.SUCCESS.success(Constants.FILE_PRE_URL + fileName);
    }
	
	@GetMapping("remove/{url:.*}")
    @ApiOperation("文件详情")
    @ApiImplicitParam(name = "文件主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity remove(@PathVariable("url") String url) {
		if (StrUtil.isEmpty(url)) {
			throw new BussinessException(ExceptionEnum.NO_PARAMS);
		}
		FileUtil.del(prePath + url);
        return ResponseEnum.SUCCESS.success();
    }
	
	@PostMapping("add")
    @ApiOperation("新增文件")
    @ApiImplicitParam(name = "文件", value = "sysFilePo", dataTypeClass = SysFilePo.class)
    public ResponseEntity add(@RequestBody SysFilePo sysFilePo) {
        return ResponseEnum.SUCCESS.success(sysFileService.save(ConvertUtil.convertObj(sysFilePo, SysFile.class)));
    }

    @PostMapping("edit")
    @ApiOperation("修改文件")
    @ApiImplicitParam(name = "文件", value = "sysFilePo", dataTypeClass = SysFilePo.class)
    public ResponseEntity edit(@RequestBody SysFilePo sysFilePo) {
        return ResponseEnum.SUCCESS.success(sysFileService.updateById(ConvertUtil.convertObj(sysFilePo, SysFile.class)));
    }

    @GetMapping("get/{id}")
    @ApiOperation("文件详情")
    @ApiImplicitParam(name = "文件主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        SysFile sysFile = sysFileService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(sysFile, SysFile.class));
    }

    @PostMapping("page")
    @ApiOperation("文件分页列表")
    @ApiImplicitParam(name = "文件Po", value = "sysFilePo", dataTypeClass = SysFilePo.class)
    public ResponseEntity page(@RequestBody SysFilePo sysFilePo) {
        return ResponseEnum.SUCCESS.success(sysFileService.page(sysFilePo));
    }
    
}
